package cn.cdtu.tibetplan.service.impl;

import java.util.List;

import cn.cdtu.tibetplan.mapper.EnrollMapper;
import cn.cdtu.tibetplan.pojo.Enroll;
import cn.cdtu.tibetplan.pojo.EnrollExample;
import cn.cdtu.tibetplan.pojo.PageResult;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import cn.cdtu.tibetplan.pojo.EnrollExample.Criteria;
import com.github.pagehelper.PageHelper;

import cn.cdtu.tibetplan.service.EnrollService;

import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class EnrollServiceImpl implements EnrollService {

    @Autowired
    private EnrollMapper enrollMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Enroll> findAll() {
        return enrollMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Enroll> page = (Page<Enroll>) enrollMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Enroll enroll) {
        enrollMapper.insert(enroll);
    }


    /**
     * 修改
     */
    @Override
    public void update(Enroll enroll) {
        enrollMapper.updateByPrimaryKey(enroll);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Enroll findOne(Long id) {
        return enrollMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            enrollMapper.deleteByPrimaryKey(id.intValue());
        }
    }


    @Override
    public PageResult findPage(Enroll enroll, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        EnrollExample example = new EnrollExample();
        Criteria criteria = example.createCriteria();

        if (enroll != null) {
            if (enroll.getName() != null && enroll.getName().length() > 0) {
                criteria.andNameLike("%" + enroll.getName() + "%");
            }
            if (enroll.getCourse() != null && enroll.getCourse().length() > 0) {
                criteria.andCourseLike("%" + enroll.getCourse() + "%");
            }
            if (enroll.getSchoolname() != null && enroll.getSchoolname().length() > 0) {
                criteria.andSchoolnameLike("%" + enroll.getSchoolname() + "%");
            }
            if (enroll.getTelephone() != null && enroll.getTelephone().length() > 0) {
                criteria.andTelephoneLike("%" + enroll.getTelephone() + "%");
            }
            if (enroll.getQq() != null && enroll.getQq().length() > 0) {
                criteria.andQqLike("%" + enroll.getQq() + "%");
            }
            if (enroll.getDemandtitle() != null && enroll.getDemandtitle().length() > 0) {
                criteria.andDemandtitleLike("%" + enroll.getDemandtitle() + "%");
            }
            if (enroll.getOther1() != null && enroll.getOther1().length() > 0) {
                criteria.andOther1Like("%" + enroll.getOther1() + "%");
            }
            if (enroll.getOther2() != null && enroll.getOther2().length() > 0) {
                criteria.andOther2Like("%" + enroll.getOther2() + "%");
            }
            if (enroll.getOther3() != null && enroll.getOther3().length() > 0) {
                criteria.andOther3Like("%" + enroll.getOther3() + "%");
            }

        }

        Page<Enroll> page = (Page<Enroll>) enrollMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

}
